package spc.webos.queue;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import spc.webos.cache.ICache;
import spc.webos.queue.ibmmq.MQManager;
import spc.webos.thread.ThreadPool;
import spc.webos.util.JsonUtil;
import spc.webos.util.StringX;

public abstract class AccessTPool extends ThreadPool {
	protected ICache msgIdCache;
	protected int cnnHoldTime = -1;

	protected int waitInterval = -1;
	protected int rw = 0;
	protected boolean keepQueue = false;
	protected String bufferName;
	protected String qname;
	protected IOnMessage onMessage;
	protected int bizPoolSize;
	protected int maxBlockSize = 0;
	protected byte[] messageId;
	protected Hashtable props;
	protected boolean correlation = false;
	protected boolean expiry = false;
	public static final int DEFAULT_EXPIRY = 120;
	public static final int DEFAULT_WAIT_INTERVAL = 4000;

	public boolean isExpiry() {
		return this.expiry;
	}

	public void setExpiry(boolean expiry) {
		this.expiry = expiry;
	}

	public void init() throws Exception {
		if (this.bizPoolSize > 0) {
			if (this.onMessage == null)
				throw new Exception("bizPoolSize:" + this.bizPoolSize
						+ ", onMessage is null!!!");
			this.onMessage = new BizPoolOnMessage(this.bizPoolSize,
					this.onMessage, this.maxBlockSize);
		}
		super.init();
	}

	public boolean isCorrelation() {
		return this.correlation;
	}

	public void setCorrelation(boolean correlation) {
		this.correlation = correlation;
	}

	public int getRw() {
		return this.rw;
	}

	public void setRw(int rw) {
		this.rw = rw;
	}

	public String getBufferName() {
		return this.bufferName;
	}

	public void setBufferName(String bufferName) {
		this.bufferName = bufferName;
	}

	public void setBufferNames(List bufferNames) {
		this.bufferName = ((String) bufferNames.get(0));
	}

	public Hashtable getProps() {
		return this.props;
	}

	public String getQname() {
		return this.qname;
	}

	public void setQname(String qname) {
		this.qname = qname;
	}

	public void setBuf2queueMapping(Map buf2queueMapping) {
		this.qname = buf2queueMapping.values().iterator().next().toString();
	}

	public void setProps(Hashtable props) {
		this.props = props;
	}

	public void setMsgIdCache(ICache msgIdCache) {
		this.msgIdCache = msgIdCache;
	}

	public int getCnnHoldTime() {
		return this.cnnHoldTime;
	}

	public void setCnnHoldTime(int cnnHoldTime) {
		this.cnnHoldTime = cnnHoldTime;
	}

	public int getWaitInterval() {
		return this.waitInterval;
	}

	public void setWaitInterval(int waitInterval) {
		this.waitInterval = waitInterval;
	}

	public IOnMessage getOnMessage() {
		return this.onMessage;
	}

	public void setOnMessage(IOnMessage onMessage) {
		this.onMessage = onMessage;
	}

	public void setLocation(String location) {
		setLocation((Map) JsonUtil.json2obj(location));
	}

	public void setLocation(Map m) {
		if (m.containsKey("name"))
			setName((String) m.get("name"));
		setSize(Integer.parseInt(StringX.null2emptystr(m.get("size"), "1")));
		setBizPoolSize(Integer.parseInt(StringX.null2emptystr(
				m.get("bizPoolSize"), "0")));
		setMaxBlockSize(Integer.parseInt(StringX.null2emptystr(
				m.get("maxBlockSize"), "-1")));
		if (m.containsKey("logName"))
			setLogName((String) m.get("logName"));
		setRw(Integer.parseInt(StringX.null2emptystr(m.get("rw"), "0")));
		if (m.containsKey("qname"))
			setQname((String) m.get("qname"));
		Object p = m.get("props");
		if (p == null)
			p = m.get("channel");
		if (p instanceof Map) {
			setProps(new Hashtable((Map) p));
		} else
			setProps(MQManager.str2map(p.toString()));
	}

	public byte[] getMessageId() {
		return this.messageId;
	}

	public void setMessageId(byte[] messageId) {
		this.messageId = messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId.getBytes();
	}

	public boolean isKeepQueue() {
		return this.keepQueue;
	}

	public void setKeepQueue(boolean keepQueue) {
		this.keepQueue = keepQueue;
	}

	public void setBizPoolSize(int size) {
		this.bizPoolSize = size;
	}

	public int getMaxBlockSize() {
		return this.maxBlockSize;
	}

	public void setMaxBlockSize(int maxBlockSize) {
		this.maxBlockSize = maxBlockSize;
	}

	public int getBizPoolSize() {
		return this.bizPoolSize;
	}
}
